// pages/all-order/order.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select:0,
    order:111,


    currentpage: 1,
    nogd: false,
    canjiazai: true
    // screenshow:'',
  },




  totop: function (e) { // 一键回到顶部
    this.setData({
      topNum: this.data.topNum = 0
    });
  },


  lower: function () {

    if (this.data.canjiazai) {
      this.setData({
        canjiazai: false
      })
      if (this.data.nogd == false) {
        var currentpage = this.data.currentpage
        console.log(currentpage)

        this.getorder(currentpage + 1)

      } else {
        console.log("没有更多")
      }

    } else {
      console.log("不能加载")
    }
  },


  // 获取全部订单
  getorder:function(page){
    ajax('/api/user/order_list', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        page:page,
        limit:20,
        status: this.data.select
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      if (page == 1) {
        if (res.data.length > 0) {

    
          this.setData({
            order: res.data
          })
        } else {
       
          this.setData({
            nogd: true,
            order: res.data
          })
        }
        if (res.data.length < 20 && res.data.length > 0) {
          this.setData({
            nogd: true,
          })
        }

      } else {

        var order = this.data.order

        if (res.data.length > 0) {



          for (var x in res.data) {
            order.push(res.data[x])
          }
          this.setData({
            order: order,
            currentpage: this.data.currentpage + 1,
            canjiazai: true,
          })
        } else {
          console.log(res.data.length)
          this.setData({
            nogd: true,
          })
        }
        if (res.data.length < 20 && res.data.length > 0) {
          this.setData({
            nogd: true,
          })
        }
      }

  
    })
  },
  // 取消订单
  cancelorder:function(e){
    var id = e.currentTarget.dataset.id;
    ajax('/api/user/order_feat', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        order_id: id,
        status:1,
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      if (res.error == '0'){
        this.getorder(1)
      }
    })
  },
  // 支付
  zhifu:function(e){
    var order_id=e.currentTarget.dataset.id
    var order_type = e.currentTarget.dataset.ordertype;
    console.log(order_type,123)
    if (order_type != 3){
      ajax('/api/user/order_feat', {
        data: {
          uqid: wx.getStorageSync("uqid"),
          order_id: order_id,
          status: 2,
        },
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "post",
      }, res => {
        console.log('去支付', res.data)
        if (res.error == '200') {
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 3000
          })
        } else if (res.error == '0') {
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success(res) {
              console.log(res)
              wx.redirectTo({
                url: '/pages/my-order/order',
              })
            },
            fail(res) {
              console.log(res)
            }
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'success',
            duration: 3000
          })
        }

      })
    }else{
      wx.navigateTo({
        url: '/pages/sure-order/order?patid=' + order_id,
      })
    }
    
  },
  // 确认收货
  shouhuo:function(e){
    var id = e.currentTarget.dataset.id;
    ajax('/api/user/order_feat', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        order_id: id,
        status: 4,
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      if (res.error == '0') {
        this.getorder(1)
      }
    })
  },
  // 选择
  active:function(e){
    var idx = e.currentTarget.dataset.index;

    this.setData({
      select: idx,
      order:111
    })


    this.setData({
      currentpage: 1,
      nogd: false,
      order: 111,
      canjiazai: true
    })
    this.totop()

    this.getorder(1)
  },
  // 去评论
  qupinglun:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/shop-comment/comment?id=' + id,
    })
  },
  // 跳转订单详情
  details:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/order-details/details?id=' + id,
    })
  },
  // 申请售后
  after:function(e){
    var orderid = e.currentTarget.dataset.orderid
    var shopid = e.currentTarget.dataset.shopid
    wx.navigateTo({
      url: '/pages/refund-after/after?orderid=' + orderid + '&shopid=' + shopid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var select=options.select

    this.setData({
      select:select
    })
    // this.getorder(1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    this.setData({
      currentpage: 1,
      nogd: false,
      order: 111,
    })
    this.getorder(1)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})